package view;

import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;

import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.StageStyle;

import java.io.IOException;
import java.util.Vector;

public class MsgListItemView extends Window {

    private AnchorPane pane;

    private Vector<MenuItem> items;

    private int type;

    public static int TYPE_FRIENDS = 1;
    public static int TYPE_GROUP = 2;

    public MsgListItemView(String name, String msg, int type) throws IOException {
        root = FXMLLoader.load(getClass().getClassLoader().getResource("msgListItem.fxml"));
        Scene scene = new Scene(root, 185, 60);
        scene.setFill(Color.TRANSPARENT);
        setScene(scene);
        initStyle(StageStyle.TRANSPARENT);
        setResizable(false);
        setTitle("msgListItem");

        pane = (AnchorPane) findViewById("rootPane");

        setName(name);
        setMsg(msg);

        this.type = type;
        setMenuItem(type);
    }

    private void setMenuItem(int type) {
        items = new Vector<>();
        if (type == TYPE_FRIENDS) {
            items.add(new MenuItem("设置备注"));
            items.add(new MenuItem("好友资料"));
            items.add(new MenuItem("删除好友"));
        }
        if (type == TYPE_GROUP) {
            items.add(new MenuItem("设置备注"));
            items.add(new MenuItem("查看资料"));
            items.add(new MenuItem("删除群聊"));
        }
        if (!items.isEmpty()) {
            ContextMenu menu = new ContextMenu();
            for (int i=0; i<items.size(); i++) {
                menu.getItems().add(items.get(i));
            }
            ((Label)findViewById("label")).setContextMenu(menu);
        }
    }

    public void setHeadPortrait(String img) {
        ((ImageView)findViewById("image")).setStyle("-fx-image: url(" + img + ");");
    }

    public void setOnlineState(int state) {
        Label label = (Label) findViewById("state");
        if (type == TYPE_GROUP) {
            label.setVisible(false);
        }

        if (state >= 1) {
            label.setText("在线");
            label.setStyle("-fx-text-fill: #7b9dfa");
        } else {
            label.setText("离线");
            label.setStyle("-fx-text-fill: grey");
        }
    }

    public void setMsg(String msg) {
        ((Label)findViewById("msg")).setText(msg);
    }

    private void setName(String name) {
        ((Label)findViewById("name")).setText(name);
    }

    public AnchorPane getPane() {
        return pane;
    }

    public Vector<MenuItem> getItems() {
        return items;
    }

    @Override
    public void quit() {

    }

    @Override
    public void minimize() {

    }
}
